package com.appkubes.mall.restful.application.system.upload.controller;

import com.appkubes.common.Response;
import com.appkubes.common.exception.BusinessException;
import com.appkubes.common.utils.data.date.DateUtils;
import com.appkubes.mall.core.po.MallAttachment;
import com.appkubes.mall.restful.application.common.base.BaseController;
import com.appkubes.mall.restful.application.system.upload.entity.dto.UploadDTO;
import com.appkubes.storage.service.StorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Api("上传模块")
@RestController
@RequestMapping("/api/Cupload")
public class UploadController extends BaseController {

    @Value("${ConfigValue.filePath}")
    protected String filePath;

    @Resource
    StorageService storageService;

    @ApiOperation(value = "上传文件")
    @PostMapping("/upload")
    public Object upload(@RequestParam("file") MultipartFile file) {
        //替换成自己路径
        try {
            List<String> fileList = storageService.save(file, filePath);
            if (fileList.size() == 0) {
                throw new BusinessException("文件上传失败");
            }

            UploadDTO uploadDTO = new UploadDTO();
            uploadDTO.setFileName(file.getOriginalFilename());
            uploadDTO.setFilePath(fileList.get(0));
            uploadDTO.setFullPath(imgUrl + fileList.get(0));
            return Response.success(uploadDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.error("文件上传失败");
        } finally {

        }
    }
}
